package com.yic.module.mes.dal.mysql.mo;

import java.util.*;

import cn.hutool.core.util.ObjectUtil;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.yic.framework.mybatis.core.mapper.BaseMapperX;
import com.yic.module.mes.dal.dataobject.mo.MoDO;
import com.yic.module.mes.dal.dataobject.moroute.MoRouteDO;
import com.yic.module.mes.dal.dataobject.report.ReportDO;
import org.apache.ibatis.annotations.Mapper;
import com.yic.module.mes.controller.admin.mo.vo.*;

/**
 * 生产排产 Mapper
 *
 * @author 系统管理员
 */
@Mapper
public interface MoMapper extends BaseMapperX<MoDO> {

    default PageResult<MoDO> selectPage(MoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MoDO>()
                .eqIfPresent(MoDO::getCode, reqVO.getCode())
                .eqIfPresent(MoDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(MoDO::getItemId, reqVO.getItemId())
                .eqIfPresent(MoDO::getItemProduceId, reqVO.getItemProduceId())
                .eqIfPresent(MoDO::getParentId, reqVO.getParentId())
                .eqIfPresent(MoDO::getParentCode, reqVO.getParentCode())
                .eqIfPresent(MoDO::getFactoryId, reqVO.getFactoryId())
                .eqIfPresent(MoDO::getWorkshopId, reqVO.getWorkshopId())
                .eqIfPresent(MoDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(MoDO::getMoRouteId, reqVO.getMoRouteId())
                .eqIfPresent(MoDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(MoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MoDO::getId));
    }

    default List<MoDO> selectList(MoExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<MoDO>()
                .eqIfPresent(MoDO::getCode, reqVO.getCode())
                .eqIfPresent(MoDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(MoDO::getItemId, reqVO.getItemId())
                .eqIfPresent(MoDO::getItemProduceId, reqVO.getItemProduceId())
                .eqIfPresent(MoDO::getParentId, reqVO.getParentId())
                .eqIfPresent(MoDO::getParentCode, reqVO.getParentCode())
                .eqIfPresent(MoDO::getFactoryId, reqVO.getFactoryId())
                .eqIfPresent(MoDO::getWorkshopId, reqVO.getWorkshopId())
                .eqIfPresent(MoDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(MoDO::getMoRouteId, reqVO.getMoRouteId())
                .eqIfPresent(MoDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(MoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MoDO::getId));
    }

    default List<MoFormsRespVO> getMoForms(MoFormsReqVO reqVO) {
        return selectJoinList(MoFormsRespVO.class, new MPJLambdaWrapper<MoDO>()
                .select(MoDO::getFactoryId)
                .select(MoDO::getWorkshopId)
                .select(MoDO::getId)
                .select(MoDO::getCode)
                .select(MoDO::getItemId)
                .select(MoDO::getProduceAmount)
                .select(MoDO::getBatchNo)
                .select(MoDO::getRemark)
                .select(MoDO::getReportAmount)
                .select(MoDO::getStatus)
                .selectSum(ReportDO::getReportAmount, MoFormsRespVO::getReportAmountSum)
                .select(MoRouteDO::getProcessId)
                .leftJoin(MoRouteDO.class, MoRouteDO::getMoId, MoDO::getId)
                .leftJoin(ReportDO.class, ReportDO::getMoRouteId, MoRouteDO::getId)
                .logicDelToOn()
                .eq(ObjectUtil.isNotEmpty(reqVO.getFactoryId()), MoDO::getFactoryId, reqVO.getFactoryId())
                .eq(ObjectUtil.isNotEmpty(reqVO.getWorkshopId()), MoDO::getWorkshopId, reqVO.getWorkshopId())
                .eq(ObjectUtil.isNotEmpty(reqVO.getMoCode()), MoDO::getCode, reqVO.getMoCode())
                .eq(ObjectUtil.isNotEmpty(reqVO.getBatchNo()), MoDO::getBatchNo, reqVO.getBatchNo())
                .eq(ObjectUtil.isNotEmpty(reqVO.getRemark()), MoDO::getRemark, reqVO.getRemark())
                .between(ObjectUtil.isNotEmpty(reqVO.getStartTime()) && ObjectUtil.isNotEmpty(reqVO.getEndTime()), MoDO::getStartTime, reqVO.getStartTime(), reqVO.getEndTime())
                .groupBy("factory_id", "workshop_id", "id", "process_id"));
    }
}
